import 'package:flutter/material.dart';
import '../models/word.dart';
import '../services/database_service.dart';

class LearningScreen extends StatefulWidget {
  @override
  _LearningScreenState createState() => _LearningScreenState();
}

class _LearningScreenState extends State<LearningScreen> {
  final DatabaseService _databaseService = DatabaseService();
  List<Word> _words = [];

  @override
  void initState() {
    super.initState();
    _loadWords();
  }

  Future<void> _loadWords() async {
    _words = await _databaseService.getWords();
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    if (_words.isEmpty) {
      return Scaffold(
        appBar: AppBar(title: Text('学习单词')), 
        body: Center(child: CircularProgressIndicator()),
      );
    }

    return Scaffold(
      appBar: AppBar(title: Text('学习单词')), 
      body: PageView.builder(
        itemCount: _words.length,
        scrollDirection: Axis.vertical,
        itemBuilder: (context, index) {
          final word = _words[index];
          return Padding(
            padding: const EdgeInsets.all(16.0),
            child: Card(
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      word.word,
                      style: TextStyle(fontSize: 32, fontWeight: FontWeight.bold),
                    ),
                    SizedBox(height: 20),
                    Text(
                      word.meaning,
                      style: TextStyle(fontSize: 24),
                    ),
                    if (word.example != null)
                      Padding(
                        padding: const EdgeInsets.only(top: 20),
                        child: Text(
                          '例句: ${word.example!}',
                          style: TextStyle(fontSize: 20, fontStyle: FontStyle.italic),
                        ),
                      ),
                  ],
                ),
              ),
            ),
          );
        },
      ),
    );
  }
}
